package com.action.gulimall.product.service.impl;

import com.action.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.action.gulimall.product.dao.BrandDao;
import com.action.gulimall.product.entity.BrandEntity;
import com.action.gulimall.product.service.BrandService;
import com.action.common.utils.PageUtils;
import com.action.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> wrapper= new QueryWrapper<BrandEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateStatusById(Long brandId, Integer showStatus) {
        UpdateWrapper<BrandEntity> wrapper= new UpdateWrapper<BrandEntity>();
        wrapper.eq(true, "brand_id", brandId);
        wrapper.set("show_status", showStatus);
        this.update(wrapper);
    }

    @Transactional
    @Override
    public void updateBrand(BrandEntity brand) {
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrandNameByBrandId(brand.getBrandId(),brand.getName());
        }
    }

}